Developer Documentation

QuickTime 4 API Documentation

QuickTime for Java

| Previous | Chapter Top | Next |

Playing a QuickTime Movie

Example 0.4 shows how to display any QuickTime content within a java.awt display space using the QTCanvas . It also demonstrates the use of the different resize options of the QTCanvas (with the alignment set to center it in the display space). You use the movie controller to select and then play a QuickTime movie, which can be a local file or a URL specified by the user.

You call QTSession.open() to perform a gestalt check to ensure that QuickTime is present and is initialized. This is a required call before any QuickTime Java classes can be used.

The window is the size of the movie and resizing the window will resize the movie. The QTCanvas is set to allow any size and is the central component in a java.awt.BorderLayout of its parent Frame .

You use the following methods to lay out and resize the Frame to the size of the Movie :

pm.pack();
pm.show();
pm.toFront();

You now prompt the user to select a movie file:

            QTFile qtf = QTFile.standardGetFilePreview(QTFile.kStandardQTFileTypes);

You open the selected file and make a movie from it, using these calls:

OpenMovieFile movieFile = OpenMovieFile.asRead(qtf);
Movie m = Movie.fromFile (movieFile);

You construct a movie controller from the resultant movie, enabling the keys so the user can interact with the movie by using the keyboard:

MovieController mc = new MovieController (m);
mc.setKeysEnabled (true);

You create a QTCanvas so that the MovieController has somewhere to draw and add it to the Frame :

QTCanvas myQTCanvas = new QTCanvas();
add (myQTCanvas);

You construct the QTDrawable object to present a movie controller:

QTPlayer myQTPlayer = new QTPlayer (mc);

Now you set it as the drawing client of the QTCanvas for a QTPlayer . This also registers interests for both mouse and key events that originate in the QTCanvas :

myQTCanvas.setClient (myQTPlayer, true);

You add a WindowListener to this frame that will close down the QTSession . Finally, you dispose of the Frame that closes down the window and you exit:

addWindowListener(new WindowAdapter () {
    public void windowClosing (WindowEvent e) {
        QTSession.close();
        dispose();
    }

    public void windowClosed (WindowEvent e) {
        System.exit(0);
    }
});

When the user closes the window, the program quits, first calling QTSession.close to terminate QuickTime. You need to call QTSession.close() if you have previously called QTSession.open() in order to shut down QuickTime properly. QTSession.close() is called before the canvas that the QuickTime object is attached to is disposed of. This enables QuickTime to clean up its graphics objects, which it attaches to the native implementation of the QTCanvas .

PlayMove.java

import java.awt.*;
import java.awt.event.*;

import quicktime.*;
import quicktime.io.*;
import quicktime.std.movies.*;
import quicktime.app.display.QTCanvas;
import quicktime.app.players.QTPlayer;

public class PlayMovie extends Frame {  
    
    public static void main (String args[]) {
        try {
            QTSession.open ();          
            PlayMovie pm = new PlayMovie("QT in Java");
            pm.pack();
            pm.show();
            pm.toFront();
        } catch (QTException e) {
                // handle errors
                . . .
        }
    }

    PlayMovie (String title) throws QTException {
        super (title);
            
        QTFile qtf = QTFile.standardGetFilePreview(QTFile.kStandardQTFileTypes);
            
        OpenMovieFile movieFile = OpenMovieFile.asRead(qtf);
        Movie m = Movie.fromFile (movieFile);
            
        MovieController mc = new MovieController (m);
        mc.setKeysEnabled (true);
            
        QTCanvas myQTCanvas = new QTCanvas();
        add (myQTCanvas);
        
        QTPlayer myQTPlayer = new QTPlayer (mc);
        
        myQTCanvas.setClient (myQTPlayer, true);
        
        addWindowListener(new WindowAdapter () {
            public void windowClosing (WindowEvent e) {
                QTSession.close();
                dispose();
            }

            public void windowClosed (WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

© 1999 Apple Computer, Inc.

| Previous | Chapter Top | Next |